package com.xm.xmapp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type.F_Main;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.customview.DateTimePickDialog;
import com.xm.xmapp.customview.NameDialog;
import com.xm.xmapp.customview.TipDialog;
import com.xm.xmapp.fragment.MainLeftFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Change_user_infoRequest;
import com.xm.xmapp.request.bean.Get_user_infoRequest;
import com.xm.xmapp.request.bean.LogoutRequest;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.MyCache;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class ChangeUserInfoActivity extends BaseActivity implements OnClickListener {

	private CustomLayout cl_name, cl_sex, cl_birthday, cl_email, cl_phone, cl_card, cl_qq;
	private TextView tuichu, right_title;
	private ImageView right;
	private MyDialogButton in;
	private Change_user_infoRequest requestBean;
	private NameDialog sexAndNameDialog, sexAndNameDialog1;
	private ResponseUserBean base;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_changeuserinfo);

	}

	@Override
	public void initView() {

		cl_name = (CustomLayout) findViewById(R.id.cl_name);
		cl_sex = (CustomLayout) findViewById(R.id.cl_sex);
		cl_birthday = (CustomLayout) findViewById(R.id.cl_birthday);
		cl_email = (CustomLayout) findViewById(R.id.cl_email);
		cl_phone = (CustomLayout) findViewById(R.id.cl_phone);
		cl_card = (CustomLayout) findViewById(R.id.cl_card);
		cl_qq = (CustomLayout) findViewById(R.id.cl_qq);
		tuichu = (TextView) findViewById(R.id.tuichu);
		right_title = (TextView) findViewById(R.id.right_title);
		right = (ImageView) findViewById(R.id.right);
	}

	@Override
	public void initData() {
		setMyTitle("个人信息");
		right.setVisibility(View.GONE);
		right_title.setVisibility(View.VISIBLE);
		right_title.setText("保存");
		right_title.setTextColor(Color.WHITE);
		requestBean = new Change_user_infoRequest();
		getData();
		in = new MyDialogButton() {
			/**
			 * flag ==3为生日4为QQ
			 */
			@Override
			public void setTitle(int flag, String str) {
				if (sexAndNameDialog != null)
					sexAndNameDialog.dismiss();

				switch (flag) {
				case 1:
					cl_name.setText2(str);
					requestBean.user_nickname = str;
					break;
				case 2:
					cl_sex.setText2(str);
					// sex 选填 Int 性别，0=保密；1=男；2=女
					if (str.equals("男"))
						requestBean.sex = 1;
					else
						requestBean.sex = 2;
					break;
				case 3:
					cl_birthday.setText2(str);
					requestBean.birthday = str;
					break;

				case 4:
					cl_qq.setText2(str);
					requestBean.qq = str;
					break;

				default:
					break;
				}
			}
		};

	}

	private void getData() {

		Get_user_infoRequest requestBean = new Get_user_infoRequest();
		promptDialog.show();
		GetDataInterFace<String> listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				base = JSON.parseObject(response, ResponseUserBean.class);
				if (base.user_nickname == null || base.user_nickname.equals(""))
					cl_name.setText2(" ");
				else
					cl_name.setText2(base.user_nickname);
				ChangeUserInfoActivity.this.requestBean.qq = base.qq;
				ChangeUserInfoActivity.this.requestBean.address = base.address;
				ChangeUserInfoActivity.this.requestBean.sex = base.sex;
				ChangeUserInfoActivity.this.requestBean.birthday = base.birthday;
				ChangeUserInfoActivity.this.requestBean.user_nickname = base.user_nickname;
				switch (base.sex) {
				case 0:
					cl_sex.setText2("保密");
					break;
				case 1:
					cl_sex.setText2("男");
					break;
				case 2:
					cl_sex.setText2("女");
					break;

				default:
					break;
				}

				if (base.birthday == null || base.birthday.equals(""))
					cl_birthday.setText2(" ");
				else
					cl_birthday.setText2(base.birthday);

				if (base.email == null || base.email.equals(""))
					cl_email.setText2(" ");
				else
					cl_email.setText2(base.email);

				String user_phone = base.user_phone + "";
				user_phone = user_phone.substring(0, 3) + "****" + user_phone.substring(7, 11);
				cl_phone.setText2(user_phone);
				if (base.qq == null || base.qq.equals(""))
					cl_qq.setText2(" ");
				else
					cl_qq.setText2(base.qq);

				if (base.car_card_printing_number == null || base.car_card_printing_number.equals("")) {
					cl_card.setOnClickListener(ChangeUserInfoActivity.this);
					cl_card.setText2("未领取");
				} else {
					cl_card.setText2(base.car_card_printing_number);
				}

			}
		};

		/**
		 * 先显示缓存数据
		 */
		String response = MyCache.getMyCache().getCache(requestBean.getCacheString());
		if (response != null && !response.equals("")) {
			listener.onResponse(response);
		}
		MyAppcation.getMyAppcation().getPostData(this, requestBean, listener, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void initEvent() {

		cl_name.setOnClickListener(this);
		cl_sex.setOnClickListener(this);
		cl_birthday.setOnClickListener(this);
		cl_email.setOnClickListener(this);
		cl_qq.setOnClickListener(this);
		cl_card.setOnClickListener(this);
		tuichu.setOnClickListener(this);
		right_title.setOnClickListener(this);
	}

	public void logoutRequest() {

		LogoutRequest requestBean = new LogoutRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				MyAppcation.getMyAppcation().clearUserBean();
				// MyAppcation.getMyAppcation().reMoveAllBaseInterFace();
				intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
				startActivity(intent);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.tuichu:

			TipDialog tipDialog = new TipDialog(this, "温馨提示", "您确定退出登录吗?", new MyDialogButton() {

				@Override
				public void setTitle(int flag, String str) {
					logoutRequest();
				}
			});
			tipDialog.show();

			break;
		case R.id.cl_name:

			sexAndNameDialog = new NameDialog(this, "请输入你的昵称", in, InputType.TYPE_CLASS_TEXT);
			sexAndNameDialog.show();
			break;
		case R.id.cl_sex:
			DateTimePickDialog dateTimePickDialog = new DateTimePickDialog(this, in, 2);
			dateTimePickDialog.show();
			break;
		case R.id.cl_birthday:
			DateTimePickDialog dateTimePickDialog1 = new DateTimePickDialog(this, in, 3);
			dateTimePickDialog1.show();
			break;

		case R.id.cl_card:
			Tools.showMessage("审核通过后，营业点领取");
			break;
		case R.id.cl_qq:
			sexAndNameDialog1 = new NameDialog(this, "请输入你的QQ", new MyDialogButton() {

				@Override
				public void setTitle(int flag, String str) {
					if (StringTools.checkQQ(str)) {
						cl_qq.setText2(str);
						ChangeUserInfoActivity.this.requestBean.qq = str;
						sexAndNameDialog1.dismiss();
					} else {
						Tools.showMessage("请输入正确的QQ号码");
					}
				}
			}, InputType.TYPE_CLASS_NUMBER);

			sexAndNameDialog1.show();
			if (this.requestBean.qq != null)
				sexAndNameDialog1.setContent(this.requestBean.qq.toString());
			break;

		case R.id.right_title:
			if (!isChange())
				change_user_infoRequest();
			else {
				finish();
			}
			break;
		default:
			break;
		}
	}

	private boolean isChange() {

		if (base == null)
			return false;

		boolean isChange = true;

		if (!base.qq.equals(requestBean.qq.toString()))
			isChange = false;

		if (!base.address.equals(requestBean.address.toString()))
			isChange = false;

		if (!base.user_nickname.equals(requestBean.user_nickname.toString()))
			isChange = false;

		if (!base.birthday.equals(requestBean.birthday.toString()))
			isChange = false;

		if (base.sex != requestBean.sex)
			isChange = false;

		return isChange;

	}

	private void change_user_infoRequest() {

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				Tools.showMessage("保存成功");
				if (requestBean.user_nickname != null && !requestBean.user_nickname.toString().equals("")) {
					ResponseUserBean bean = MyAppcation.getMyAppcation().getUserBean();
					bean.user_nickname = (String) requestBean.user_nickname;
					MyAppcation.getMyAppcation().saveUserBean(bean);
				}
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainLeftFragment.class, F_Main.DEFAULT);
				finish();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
			}
		});
	}

}
